package com.woniu113.junk.order.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu113.junk.commons.model.Result;
import com.woniu113.junk.commons.util.RedisUtil;
import com.woniu113.junk.commons.util.TokenUtil;
import com.woniu113.junk.order.dto.AllPerformanceInfoDTO;
import com.woniu113.junk.order.dto.PerformanceInfoDTO;
import com.woniu113.junk.order.form.performanceInfo.FindAllPerformanceForm;
import com.woniu113.junk.order.form.performanceInfo.FindByRiderIdForm;
import com.woniu113.junk.order.model.PerformanceInfo;
import com.woniu113.junk.order.param.performanceInfo.FindAllPerformanceParam;
import com.woniu113.junk.order.param.performanceInfo.FindByRiderIdParam;
import com.woniu113.junk.order.service.PerformanceInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@RestController
@RequestMapping("/performanceInfo")
@Api(tags = "绩效信息管理")
public class PerformanceInfoController {

    @Resource
    private PerformanceInfoService pis;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 处理骑手的正常订单统计请求.
     *
     * @param orderNum 订单编号
     * @return Result<PerformanceInfo> 包含骑手正常订单统计结果的响应
     * @author 小周
     * @date 2024/10/22
     */
    @GetMapping("/normalAdd/{orderNum}")
    @ApiOperation(value = "统计骑手正常订单", notes = "根据订单编号统计骑手的正常订单数量")
    public Result<PerformanceInfo> normalAdd(
            @ApiParam(value = "订单编号", required = true) @PathVariable String orderNum) {
        pis.normalAdd(orderNum);
        return Result.getResult(PerformanceInfo.class).setCode(200).setMsg("骑手正常订单统计成功");
    }

    /**
     * 处理骑手的异常订单统计请求.
     *
     * @param orderNum 订单编号
     * @return Result<PerformanceInfo> 包含骑手异常订单统计结果的响应
     * @author 小周
     * @date 2024/10/22
     */
    @GetMapping("/abnormalAdd/{orderNum}")
    @ApiOperation(value = "统计骑手异常订单", notes = "根据订单编号统计骑手的异常订单数量")
    public Result<PerformanceInfo> abnormalAdd(
            @ApiParam(value = "订单编号", required = true) @PathVariable String orderNum) {
        pis.abnormalAdd(orderNum);
        return Result.getResult(PerformanceInfo.class).setCode(200).setMsg("骑手异常订单统计成功");
    }

    /**
     * 查询当前登录骑手账号的个人订单绩效.
     *
     * @param form  包含查询条件的表单数据，包含骑手相关信息
     * @param token  用于验证身份的请求头
     * @return Result<Page<PerformanceInfo>> 返回查询结果的分页数据，包括骑手的个人订单绩效信息
     * @throws Exception 当解析 token 或查询过程中发生错误时抛出异常
     * @author 小周
     * @date 2024/10/22
     */
    @PostMapping("/findByRiderId")
    @ApiOperation(value = "查询骑手个人订单绩效", notes = "根据骑手ID查询当前登录骑手的个人订单绩效")
    public Result<Page<PerformanceInfo>> findByRiderId(
            @ApiParam(value = "查询条件表单", required = true) @RequestBody FindByRiderIdForm form,
            @ApiParam(value = "身份验证token", required = true) @RequestHeader String token) throws Exception {
        int adminId = Integer.parseInt(TokenUtil.parseToken(redisUtil.get(token)).get("userId").toString());
        FindByRiderIdParam param = BeanUtil.toBean(form, FindByRiderIdParam.class);
        param.setRiderId(adminId);
        Page<PerformanceInfo> page = pis.findByRiderId(param);
        return Result.getPageResult(PerformanceInfo.class).setCode(200).setMsg("查询当前登录骑手账号个人订单绩效成功").setData(page);
    }


    /**
     * 查询所有骑手的绩效信息
     */
    @PostMapping("/findAll")
    @ApiOperation(value = "查询骑手全部绩效信息", notes = "查询骑手全部绩效信息")
    public Result<List<PerformanceInfoDTO>> findAll(@RequestBody FindAllPerformanceForm form){
        System.out.println(form.getAdminName());
        System.out.println(form.getPageSize());
        System.out.println(form.getPageNum());
        AllPerformanceInfoDTO all = pis.findAll(BeanUtil.toBean(form, FindAllPerformanceParam.class));
        return Result.getListResult(PerformanceInfoDTO.class).setCode(200).setMsg("查询骑手全部绩效信息成功").setData(all.getList()).setTotal(all.getTotal());
    }
}

